package crm.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;

import tutorial.support.JTutorialFrame;
import bibliothek.extension.gui.dock.theme.EclipseTheme;
import bibliothek.gui.DockController;
import bibliothek.gui.dock.DefaultDockable;
import bibliothek.gui.dock.SplitDockStation;
import bibliothek.gui.dock.station.split.SplitDockProperty;
import crm.model.CRMConst;
import crm.model.teacherPC.CRMListener;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.net.StudentPC;
import crm.util.treetable.JTreeTable;
import crm.view.desktop.StudentDesktopPane;
import crm.view.funktionBar.FunktionBar;
import crm.view.setting.SettingPane;
import crm.view.statistic.StatisticTree;
import crm.view.statistic.treetable.StatisticTreeTableModel;
import crm.view.tree.students.ConnectStudentTreeView;
import crm.view.tree.verwaltung.EveryStundetsTreeControll;
import crm.view.tree.verwaltung.StudentTreeModel;
import crm.view.tree.verwaltung.StudentTreeRender;
import crm.view.tree.verwaltung.StudentTreeView;

public class DockingFrame extends JTutorialFrame implements WindowListener, CRMListener {
	
	Manager manager;
	JScrollPane studentTreeViewComp;
	JPanel everyStudentComp;
	StudentDesktopPane desktops;
	StatisticTree statisticTree;
	JTreeTable treetable;
	SettingPane settingPane;
	FunktionBar fBar;
	public DockingFrame(Manager manager) {
		super("JCRM");
		this.manager=manager;
		addWindowListener(this);
		manager.addListener(this);
		
		initGraphics();
		
		DockController controller = new DockController();
		controller.setRootWindow( this );
		destroyOnClose( controller );
		controller.setTheme( new EclipseTheme() );
		
		SplitDockStation  leftSplitt = new SplitDockStation();
		leftSplitt.setTitleText("JCRM");
		
		
		leftSplitt.drop(new DefaultDockable(studentTreeViewComp, "Student"));
		leftSplitt.drop(new DefaultDockable(everyStudentComp, "every PC's"), new SplitDockProperty(0, 0, 1.0, 1.0 ));
		

		leftSplitt.drop(new DefaultDockable(desktops, "Student"),new SplitDockProperty( 0.3, 0.0, 0.7, 1.0 ));
		leftSplitt.drop(new DefaultDockable(statisticTree, "every PC's"),new SplitDockProperty( 0.3, 0.0, 0.7, 1.0 ));
		leftSplitt.drop(new DefaultDockable(treetable, "every PC's"),new SplitDockProperty( 0.3, 0.0, 0.7, 1.0 ));
		leftSplitt.drop(new DefaultDockable(settingPane, "every PC's"),new SplitDockProperty( 0.3, 0.0, 0.7, 1.0 ));
		
		controller.add(leftSplitt);
		add(leftSplitt, BorderLayout.CENTER);
		add(fBar, BorderLayout.NORTH);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 

		
		
//		add(centerSplitt);
		
		setVisible(true);
	}
	
	
	public void initGraphics(){
		
		// StudentTreeView - studentTreeViewComp
		studentTreeViewComp = new JScrollPane();
		studentTreeViewComp.setPreferredSize(new java.awt.Dimension(139, 372));
		
		JTree jTree1 = new StudentTreeView(new StudentTreeModel(manager), manager);
		studentTreeViewComp.setViewportView(jTree1);
		jTree1.setCellRenderer(new StudentTreeRender());
		
		// Every Student - everyStudentComp
		
		
			everyStudentComp = new JPanel();
			BorderLayout jPanel3Layout = new BorderLayout();
			everyStudentComp.setLayout(jPanel3Layout);
//			jTabbedPane1.addTab("Alle PCs", null, jPanel3, null);
			everyStudentComp.setPreferredSize(new java.awt.Dimension(139, 324));
			
			ConnectStudentTreeView everyStudents = new ConnectStudentTreeView(manager);
			everyStudentComp.add(new JScrollPane(everyStudents), BorderLayout.CENTER);

			
			
			JPanel jPanel4 = new JPanel();
			everyStudentComp.add(jPanel4, BorderLayout.SOUTH);
			FlowLayout jPanel4Layout = new FlowLayout();
			jPanel4.setLayout(jPanel4Layout);
			EveryStundetsTreeControll con = new EveryStundetsTreeControll(everyStudents, manager);
				
			JButton addBut = new JButton();
			addBut.addActionListener(con);
			jPanel4.add(addBut);
			addBut.setText("add");
				
				
			JButton jButton4 = new JButton();
			jButton4.addActionListener(con);

			jPanel4.add(jButton4);
			jButton4.setText("remove");
				
			
			//  StudentDesktopPane - desktops
			desktops = new StudentDesktopPane(manager);
		
			// StatisticTree
			statisticTree = new StatisticTree(manager);
			
			
			// Treetable
			treetable = new JTreeTable(new StatisticTreeTableModel(manager));
			
			//Settings
			settingPane = new SettingPane(this, manager);
			
			
			fBar = new FunktionBar(manager);
			
			
	}
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		CRMConst.connected = false;
	}

	@Override
	public void windowClosing(WindowEvent arg0) {


	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}


	public void stateChanged(ChangeEvent ev) {
		JSpinner spin = (JSpinner) ev.getSource();

	}





	@Override
	public void actualisingComplete(StudentPC student) {

	}
	@Override
	public void connectionClosed() {


	}
	@Override
	public void connectionStarted() {

	}

	@Override
	public void exitCRM() {
		dispose();
	}



	@Override
	public void studentAdded(StudentPC student) {
		// TODO Auto-generated method stub

	}



	@Override
	public void studentRemoved(StudentPC student) {
		// TODO Auto-generated method stub

	}
}
